package com.approval.approvalapp.index.cygn.wzly;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WzlyListBean;
import com.approval.approvalapp.util.ToastUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by louyulin on 2019/5/7.
 */

public class RrvAdapter extends RecyclerView.Adapter<RrvAdapter.Holder> {

    List<WzlyListBean.ListBean> littlelist;
    Context context;
    RrvClickListener listener;

    public void setLittlelist(List<WzlyListBean.ListBean> littlelist, RrvClickListener listener) {
        this.littlelist = littlelist;
        this.listener = listener;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.item_rrv, parent, false
        );
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        holder.titleTv.setText(littlelist.get(position).getName());
        if (littlelist.get(position).getCurrent_count() == 0) {
            holder.numTv.setTextColor(Color.parseColor("#FF999999"));
            holder.subtractIv.setVisibility(View.GONE);
        } else {
            holder.numTv.setTextColor(Color.parseColor("#FF333333"));
            holder.subtractIv.setVisibility(View.VISIBLE);
        }
        if (littlelist.get(position).getCurrent_count() == Integer.valueOf(littlelist.get(position).getCount())) {
            holder.addIv.setBackgroundResource(R.drawable.add_full);
        } else {
            holder.addIv.setBackgroundResource(R.drawable.add_article);
        }
        holder.numTv.setText(littlelist.get(position).getCurrent_count() + "");
        holder.subtractIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.subtract(position);


            }
        });
        holder.addIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.add(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (littlelist == null) {
            return 0;
        } else {
            return littlelist.size();
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.title_tv)
        TextView titleTv;
        @Bind(R.id.add_iv)
        ImageView addIv;
        @Bind(R.id.num_tv)
        TextView numTv;
        @Bind(R.id.subtract_iv)
        ImageView subtractIv;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface RrvClickListener {
        public void add(int index);

        public void subtract(int index);
    }

}
